﻿using System;
using System.Collections.Generic;

using System.Linq;

#if !WINRT
using System.IO;
using System.Security.Cryptography;
#else
using System.Text;
using Windows.Security.Cryptography;
using Windows.Security.Cryptography.Core;
#endif

namespace MishraReader
{
    public static class CryptoService
    {
#if!WINRT
        private static readonly AesManaged aesCrypto = new AesManaged();
#endif
        private static readonly byte[] rgbIV = {1, 2, 5, 8, 10, 22, 33, 8, 16, 240, 87, 65, 45, 14, 65, 81};

        private static readonly byte[] rgbKey = {
                                                    9, 16, 24, 32, 64, 48, 32, 21, 12, 36, 45, 12, 13, 14, 15, 17, 9, 16, 24, 32, 64, 48, 32, 21, 12, 36, 45, 12
                                                    ,
                                                    13, 14, 15, 17
                                                };
#if !WINRT
        public static string Crypt(string source)
        {
            var crypto = aesCrypto.CreateEncryptor(rgbKey, rgbIV);

            using (var msEncrypt = new MemoryStream())
            {
                using (var csEncrypt = new CryptoStream(msEncrypt, crypto, CryptoStreamMode.Write))
                {
                    using (var swEncrypt = new StreamWriter(csEncrypt))
                    {
                        //Write all data to the stream.
                        swEncrypt.Write(source);
                    }
                    return Convert.ToBase64String(msEncrypt.ToArray());
                }
            }
        }

        public static string Decrypt(string source)
        {
            var crypto = aesCrypto.CreateDecryptor(rgbKey, rgbIV);
            using (var msDecrypt = new MemoryStream(Convert.FromBase64String(source)))
            {
                using (var csDecrypt = new CryptoStream(msDecrypt, crypto, CryptoStreamMode.Read))
                {
                    using (var srDecrypt = new StreamReader(csDecrypt))
                    {
                        return srDecrypt.ReadToEnd();
                    }
                }
            }
        }
#else
        public static string Crypt(string source)
        {
            var algorithm = SymmetricKeyAlgorithmProvider.OpenAlgorithm("AES_CBC_PKCS7");

            var keymaterial = CryptographicBuffer.CreateFromByteArray(rgbKey); 
            var key = algorithm.CreateSymmetricKey(keymaterial);

            var iv = CryptographicBuffer.CreateFromByteArray(rgbIV);
            var buffer = CryptographicBuffer.ConvertStringToBinary(source, BinaryStringEncoding.Utf8);
            var encrypted = CryptographicEngine.Encrypt(key, buffer, iv);

            return CryptographicBuffer.EncodeToBase64String(encrypted);
        }

        public static string Decrypt(string source)
        {
            var algorithm = SymmetricKeyAlgorithmProvider.OpenAlgorithm("AES_CBC_PKCS7");

            var keymaterial = CryptographicBuffer.CreateFromByteArray(rgbKey);
            var key = algorithm.CreateSymmetricKey(keymaterial);
            var iv = CryptographicBuffer.CreateFromByteArray(rgbIV);

            var sourceBuffer = CryptographicBuffer.DecodeFromBase64String(source);

            var decrypted = CryptographicEngine.Decrypt(key, sourceBuffer, iv);

            return CryptographicBuffer.ConvertBinaryToString(BinaryStringEncoding.Utf8, decrypted);
        }
#endif
    }
}